package com.shivandragon.utils;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteConcern;

public class MongoSingletonHolder {

	private static final MongoSingletonHolder INSTANCE = new MongoSingletonHolder();
	
	private final Mongo mongo;
	private DB db;
	
	{
		MongoClientURI mongoUri = new MongoClientURI(Constants.MONGO_URI);
        try {
			mongo = new MongoClient();
			db = this.mongo.getDB(Constants.MONGO_DB_NAME);
			db.setWriteConcern(WriteConcern.SAFE);
			//db.authenticate(Constants.MONGO_USER, Constants.MONGO_PASSWORD.toCharArray());
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}
	
	private MongoSingletonHolder() {}
	
	public static MongoSingletonHolder getInstance() {
		return INSTANCE;
	}
	
	public DB getDB() {
		return db;
	}
	
	public DBCollection getPresentationsDBCollection() {
		return getDB().getCollection(Constants.MONGO_PRESENATIONS_COLLECTION_NAME);
	}
}
